package com.wwz.leetcode;

import com.google.common.collect.Maps;
import org.apache.commons.collections4.CollectionUtils;
import org.checkerframework.checker.units.qual.min;
import org.junit.Test;

import java.util.*;

import com.google.common.collect.Lists;

public class LC599 {

    public String[] findRestaurant(String[] list1, String[] list2) {
        Map<String, Integer> index = new HashMap<>();
        for (int i = 0; i < list1.length; i++) {
            index.put(list1[i], i);
        }

        List<String> ret = new ArrayList<String>();
        int indexSum = Integer.MAX_VALUE;
        for (int i = 0; i < list2.length; i++) {
            if (index.containsKey(list2[i])) {
                int j = index.get(list2[i]);
                if (i + j < indexSum) {
                    ret.clear();
                    ret.add(list2[i]);
                    indexSum = i + j;
                } else if (i + j == indexSum) {
                    ret.add(list2[i]);
                }
            }
        }
        return ret.toArray(new String[ret.size()]);
    }

    @Test
    public void test() {

//        List list1 = Lists.newArrayList("Shogun", "Tapioca Express", "Burger King", "KFC");
//        List list2 = Lists.newArrayList("KFC", "Shogun", "Burger King");
//        String[] list1 = {"Shogun", "Tapioca Express", "Burger King", "KFC"};
//        String[] list2 = {"KFC", "Shogun", "Burger King"};
        String[] list1 ={"Shogun","Tapioca Express","Burger King","KFC"};
        String[] list2 = {"KFC","Burger King","Tapioca Express","Shogun"};

        String[] restaurant = findRestaurant(list1, list2);
        for (String s : restaurant) {
            System.out.println(s);
        }

    }
}
